package UI;

import UI_EJB.IUserInfo;
import UI_EJB.UserInfoEntity;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.List;
import java.util.ArrayList;


/**
 * Created by IntelliJ IDEA.
 * User: mchebanenko
 * Date: 17-Aug-2010
 * Time: 2:45:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserInfo {

    private UserInfoEntity UInfo = new UserInfoEntity();
    private List ListOfUInfo;
    private DataModel reportsModel;

    
    public DataModel getReportsModel() {
        if (reportsModel == null) {
                    reportsModel = new ListDataModel();
       }
       reportsModel.setWrappedData(getListOfUInfo());
       return reportsModel;

    }



    public List getListOfUInfo() {
         try{
            Context cont = new InitialContext();
            ListOfUInfo = ((IUserInfo) cont.lookup("HotelManager/UserInfoEntityBean/local")).getAllUsersInfo();
            return ListOfUInfo;
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return null;
        }
    }

    public void setListOfUInfo(ArrayList listOfUInfo) {
        ListOfUInfo = listOfUInfo;
    }

    public UserInfoEntity getUInfo() {
        return UInfo;
    }

    public void setUInfo(UserInfoEntity UInfo) {
        this.UInfo = UInfo;
    }

    public String SaveInfo () {
        try{
            Context cont = new InitialContext();
            int res = ((IUserInfo) cont.lookup("HotelManager/UserInfoEntityBean/local")).SaveUsersInfo(UInfo);
            if (res == 1)
                return  "Saved";
            else
                return  "Error";
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return  "Error";
        }
    }

     public void sendToAddUser (){
        try{
            this.setUInfo(null);
            FacesContext cont =  FacesContext.getCurrentInstance();
            HttpServletResponse resp = (HttpServletResponse)cont.getExternalContext().getResponse();
            resp.sendRedirect("AddUserInfo.faces");
            cont.responseComplete();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


      public void sendToEditUser (){
        try{

            this.setUInfo((UserInfoEntity)reportsModel.getRowData());
            
            FacesContext cont =  FacesContext.getCurrentInstance();
            HttpServletResponse resp = (HttpServletResponse)cont.getExternalContext().getResponse();
            resp.sendRedirect("AddUserInfo.faces");
            cont.responseComplete();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
